<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="title"
    :visible.sync="visible"
    :before-close="cancel"
    width="75%"
    top="5vh"
    lock-scroll
    :append-to-body="true"
  >
    <el-form
      class="edit-form"
      status-icon
      :model="datas"
      :rules="rules"
      @keyup.enter.native="save"
      ref="editForm"
    >
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item prop="project_id" label="项目名称" :label-width="baseStyle.width">
            <el-select
              v-model="datas.project_id"
              placeholder="请选择"
              style="width: 100%;"
              @change="changeClassic"
            >
              <el-option
                v-for="item in projectList"
                :key="item.id+item.project_name"
                :label="item.project_name"
                :value="item.id"
                :disabled="!isAdd?true:false"
              ></el-option>
            </el-select>
            <!-- <el-input v-model="formData.name" :readonly="true" autocomplete="off"></el-input> -->
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="project_no" label="项目编号" :label-width="baseStyle.width">
            <el-input v-model="datas.project_no" :readonly="true" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="class_name" label="项目类别" :label-width="baseStyle.width">
            <el-input v-model="datas.class_name" :readonly="true" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="month" label="选择月份" :label-width="baseStyle.width">
            <el-date-picker
              v-model="datas.month"
              :readonly="!isAdd?true:false"
              type="month"
              placeholder="选择月"
              value-format="yyyy-MM"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item
            prop="currentStage"
            :label="`${stageData.name?'项目阶段（'+stageData.name+'）':'项目阶段'}`"
            :label-width="baseStyle.width"
          >
            <el-date-picker
              v-if="stageData.id&&stageData.name"
              v-model="stageData.value"
              :readonly="true"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
            <div v-else>暂无</div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="main_content" label="月工作内容" :label-width="baseStyle.width">
            <el-input
              type="textarea"
              placeholder="请输入内容"
              v-model="datas.main_content"
              show-word-limit
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button :type="'primary'" @click.native="save">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { Message } from "element-ui";
import projectAPI from "@apis/newProject";
export default {
  name: "editForm",
  props: {
    title: String,
    baseStyle: Object,
    datas: {
      type: Object,
      default: {}
    },
    visible: Boolean,
    isAdd: Boolean,
    saveForm: Function,
    uid: Number
  },
  data() {
    const rules = this.getRules();

    return {
      rules,
      projectList: [],
      currentStage: "",
      stageData: { id: null, name: "", value: [] }
    };
  },
  components: {},
  computed: {},
  created() {
    this.getProjectList({ uid: this.uid });
    if (!this.isAdd) {
      this.getStageCurr({
        class: this.datas.class_id,
        id: this.datas.project_id
      });
    }
  },
  methods: {
    //立项申请的项目类别下拉框数据getPLanList
    getProjectList(data) {
      projectAPI
        .getProjectLists(data)
        .then(res => {
          this.projectList = [...res];
          if (this.datas && this.datas.project_id) {
            let curItem = res.filter(item => this.datas.project_id === item.id);
            this.datas.project_no = curItem[0].project_no;
            this.datas.class_name = curItem[0].class_name;
            this.datas.class_id = curItem[0].project_class;
          }
        })
        .catch(err => this.$message.error(err.message));
    },
    //根据类别获取当前阶段
    getStageCurr(val) {
      projectAPI
        .getStageCurrInfo(val)
        .then(res => {
          if (res && res.id) {
            this.stageData.id = res.id;
            this.stageData.name = res.name;
            this.stageData.value = [res.start_time, res.end_time];
          }
        })
        .catch(err => {
          this.stageData = { id: null, name: "", value: [] };
          if (!err.message == "获取失败！") {
            this.$message.error(err.message);
          }
        });
    },
    //选择classic时联动stage
    changeClassic(val) {
      let curItem = this.projectList.filter(item => val === item.id);
      this.getStageCurr({
        class: curItem[0].project_class,
        id: curItem[0].id
      });
      this.datas.project_no = curItem[0].project_no;
      this.datas.class_name = curItem[0].class_name;
      this.datas.class_id = curItem[0].project_class;
    },
    cancel() {
      this.$emit("update:visible", false);
      this.$emit("update:datas", {});
      this.$refs.editForm.resetFields();
    },
    save() {
      // console.log(this.datas, "form");
      // console.log(this.stageData, "this.stageData");
      let formData = {};
      this.$refs.editForm.validate(valid => {
        if (valid) {
          formData.project_id = this.datas.project_id;
          formData.class_id = this.datas.class_id;
          formData.stage_id = this.stageData.id;
          formData.main_content = this.datas.main_content
            ? this.datas.main_content
            : "";
          formData.month = this.datas.month;
          // console.log(formData, "formData");
          this.saveForm(formData);
          // this.cancel();
        } else {
          return false;
        }
      });
    },
    isEmpty(value) {
      if (!value || value.length < 1) {
        return true;
      }
      return false;
    },
    validate(id, prop, value, des) {
      if (this.isEmpty(value)) {
        return {
          success: false,
          message: `请填写${des}`
        };
      }
      return { success: true };
    },
    getRules() {
      return {
        project_no: [
          {
            validator: (rule, value, callback) => {
              const result = this.validate(
                this.datas.project_no,
                "project_no",
                this.datas.project_no,
                "项目编号"
              );
              if (!result.success) {
                callback(new Error(result.message));
                return;
              }
              callback();
            },
            trigger: ["blur", "change"]
          }
        ],
        project_id: [
          {
            validator: (rule, value, callback) => {
              const result = this.validate(
                this.datas.project_id,
                "project_id",
                this.datas.project_id,
                "项目名称"
              );
              if (!result.success) {
                callback(new Error(result.message));
                return;
              }
              callback();
            },
            trigger: ["blur", "change"]
          }
        ]
      };
    }
  }
};
</script>
<style lang="scss" scoped>
.edit-form {
  height: 55vh;
  overflow-y: auto;
  overflow-x: hidden;
}
.things-item > span {
  cursor: pointer;
  padding-right: 20px;
}
</style>>